package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.wreport.WReportBranchEntity;
import jsyf.oa.entity.wreport.WReportCostEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: @张巨波 @zhangjubo
 * @creat: on 2017/11/28.
 * @since: V1.0.0
 * @see jsyf.oa.web
 */
@Repository
public class WReportBranchDao extends BaseJdbcDao {

    public Integer add(WReportBranchEntity model) {
        model.setId(super.generateKey());
        return   super.insert("T_WRT_BRANCH", model.getJSONObject());
    }
    public Integer update(WReportBranchEntity model) {
        return   super.update("T_WRT_BRANCH", model.getJSONObject(),model.getJSONObjectId());
    }
    public List<WReportBranchEntity> getByCondition(String condition, Object[] params){
        return this.getByPage(condition,params,1,Integer.MAX_VALUE);
    }
    public List<WReportBranchEntity> getByPage(String condition,Object[] params, int requestPage, int pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" ID AS id, \n");
        sql.append(" W_ID AS wID, \n");
        sql.append(" BRANCH_NAME AS branchName, \n");
        sql.append(" EXCHANGE AS exchange  \n");
        sql.append(" FROM T_WRT_BRANCH \n");
        sql.append(" WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append(" ORDER BY SORT_NO");
        super.appendPageSql(sql,requestPage,pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(),params);

        List<WReportBranchEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            WReportBranchEntity model = JSON.toJavaObject(jsonList.get(i),WReportBranchEntity.class);
            list.add(model);
        }
        return list;
    }
    public List<WReportBranchEntity> getByWid(String id){
        List<WReportBranchEntity> list = getByCondition(" and W_ID  = ?",new String[]{id});
        return  list;
    }
}
